import {
  pickedFaceData,
  pickedLineData,
  pickedVertexData,
  PickType,
} from '@amcax/base';
import { Vector3, Vector3Tuple } from 'three-legacy';
type PickData = pickedLineData | pickedFaceData | pickedVertexData;
/**
 * 获取直线方向向量和面的法向量
 * @param pickerData 选中线和面的数据
 * @returns 返回直线方向向量和面的法向量
 */
export function getDirectionPos(pickerData: PickData[]) {
  // 首先先筛选线性和平面
  const newPickerData = pickerData.filter((p) => {
    return (
      p.idInfo.t === PickType.LINE ||
      p.idInfo.t === PickType.PLANE ||
      p.idInfo.t === PickType.VERTEX
    );
  });

  const vecArr: Vector3Tuple[] = [];
  if (newPickerData.length === 0) {
    return null;
  }
  for (const item of newPickerData) {
    if ('edgeId' in item && item.edgeData.length > 0) {
      const lineVec = new Vector3()
        .subVectors(item.edgeData[1], item.edgeData[0])
        .normalize();
      vecArr.push([lineVec.x, lineVec.y, lineVec.z]);
    } else if ('meshId' in item) {
      const faceIndex = item.meshId.split(':')[1]?.substring(1);
      const normalVec = item.modelData?.faces[faceIndex ?? 0].normals?.[0];
      if (normalVec) {
        vecArr.push(normalVec);
      }
    } else if ('vertexId' in item) {
      const positions = item.assisObj.positions;
      vecArr.push(positions as Vector3Tuple);
    }
  }
  return vecArr;
}
